//
//  CMDBVCodeManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBVCodeManager: NSObject {
    
    static let getInstance: CMDBVCodeManager = CMDBVCodeManager()
    
    private override init() {
        
    }
}
/*
 
 let vcodeParam = CMDBVCodeParam()
 
 vcodeParam.phone = "18612598269"
 
 vcodeParam.vcodeType = .Reg
 
 do {
 
 try CMDBVCodeManager.getInstance.queryVCode(vcodeParam, handler: { (isVCodePost, vCodeType) in
 
 
 
 }, error: { (rescode) in
 
 }) {
 
 
 }
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid) {
 
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeTypeUnSelected) {
 
 
 } catch {
 
 
 }
 
 
 
 */
extension CMDBVCodeManager: CMDBVCodeManagerDelegate {
    
    func queryVCode(vCodeParam: CMDBVCodeParam, handler: CMDBVCodeHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard vCodeParam.phone.validPhoneNumber() else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PhoneInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid)
        }
        guard vCodeParam.vcodeType != CMDBVCodeType.UnKnown else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.VCodeTypeUnSelected.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.VCodeTypeUnSelected)
        }
        
        let param = vCodeParam.keyValues
        
        CMDBHttpManager.getInstance.request(vCodeParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            if let _ = data {
                
                handler(isVCodePost: data as! Bool,vCodeType: vCodeParam.vcodeType)
                
            } else {
                
                handler(isVCodePost: false,vCodeType: vCodeParam.vcodeType)
                
            }
            }, error: error, failure: failure)
        
    }
}
extension CMDBVCodeManager {
    
    func verifyVCode(vCodeParam: CMDBVerifyVCodeParam, handler: CMDBVerifyVCodeHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        let param = vCodeParam.keyValues
        
        CMDBHttpManager.getInstance.request(vCodeParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            let vertifyVCodeBean = CMDBVertifyVCodeBean.objectWithKeyValues(data as! NSDictionary) as! CMDBVertifyVCodeBean
            
            handler(vertifyVCodeBean: vertifyVCodeBean)
            
            }, error: error, failure: failure)
        
    }
    
}

